package com.egao.common.component.elasticsearch.controller;

import com.egao.common.component.elasticsearch.entity.ElasticSearchResult;
import com.egao.common.component.elasticsearch.service.ElasticSearchService;
import com.egao.common.core.annotation.OperLog;
import com.egao.common.core.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * ElasticSearch 控制器
 * @author lym
 */
@Api(tags = "ElasticSearch 查询控制器")
@RestController
@RequestMapping("${api}")
public class ElasticSearchController {

    @Autowired
    private ElasticSearchService elasticSearchService;

    @ApiOperation("列表")
    @OperLog(value = "全局搜素", desc = "全局搜素")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "keyword", value = "查询内容", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "category", value = "查询类型", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "当前页", required = false, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "limit", value = "每页条数", required = false, dataType = "Integer", paramType = "query"),


    })
    @GetMapping("/publicly/es/search")
    public ElasticSearchResult list(String keyword, String category, String type, @RequestParam(name="page",defaultValue="0") Integer page) throws IOException {
        return elasticSearchService.search(category, keyword, type, page, 10);
    }

    @ApiOperation("初始化数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "category", value = "查询类型", dataType = "String", paramType = "query"),
    })
    @GetMapping("/publicly/es/init")
    public JsonResult initIndex(String category, @RequestParam(name="limit",defaultValue="2000") Integer limit) throws IOException {
        return elasticSearchService.initIndex(category, limit);
    }



}
